﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand.CustomerManager
{
    public class RetrieveCustomerNeedToUpdateServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerNeedToUpdateRequest, RetrieveCustomerNeedToUpdateResponse>
    {
        #region Properties
        public UserRecord Dao;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public FilterRecord SearchColumns = null;
        #endregion
        public RetrieveCustomerNeedToUpdateServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerNeedToUpdateRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            SearchColumns = Request.Filter;
        }

        protected override bool OnExecute(RetrieveCustomerNeedToUpdateResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<CustomerRecord> records = new List<CustomerRecord>();
                if (SearchColumns.IsExport)
                {
                    records = Providers.ExportCustomers.RetrieveCustomerNeedToUpdateInfo(out totalRecord, Dao);
                }
                else
                {

                    List<CustomerNeedUpdateInfo> listCustomer = new List<CustomerNeedUpdateInfo>();
                    Dictionary<string, BranchRecord> listBranches = Providers.Branchs.RetrieveAllBranches().Select(x => new BranchRecord
                    {
                        BranchId = x.BranchId,
                        BranchName = x.BranchNameSme,
                        Region = x.Region
                    }).ToList().ToDictionary(x => x.BranchId);
                    listCustomer = Providers.Customers.RetrieveCustomerNeedToUpdateInfoByDao(out totalRecord, Dao, SearchColumns, Request.Lang, pageSize, pageIndex);
                    if (listCustomer != null)
                    {
                        records = listCustomer.Select(x => new CustomerRecord
                        {
                            DaoName = x.DaoName == null ? string.Empty : x.DaoName,
                            Dao = x.Dao == null ? string.Empty : x.Dao,
                            Branch = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId] : new BranchRecord(),
                            CIF = x.Cif,
                            Name = x.CusName,
                            VpbIndustry = x.IndustryName,
                            VpbIndustryEn = x.IndustryNameEn,
                            NoOfInfoNeedToUpdate = x.NoInfoNeedUpdate,
                            DateOpenCif = x.CusOpenDate.Value
                        }).ToList();
                    }
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
